<?php

namespace app\controller;

use app\BaseController;
use app\Request;
use think\facade\Cache;
use think\facade\View;
use think\facade\Db;

class Admin extends Common
{
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $post = $request->post();
            $post['recate_time'] = time();
            $state = Db::table('feedback')->save($post);
            if (!empty($state)) {
                exit(json_encode(['code' => 1, 'msg' => '成功']));
            }
            exit(json_encode(['code' => 0, 'msg' => '失败']));
        }
        $type = Db::table('type')->order('id desc')->select();
        View::assign('type', $type);
        return View::fetch('index');
    }

    public function feedback(Request $request)
    {
        if ($request->isAjax()) {
            $get = $request->get();
            $page = $get['page'] - 1;
            $limit = $get['limit'];
            $feedback = Db::table('feedback')->order('id desc')->limit($page * $limit, $limit)->select();
            $count = Db::table('feedback')->count();
            exit(json_encode(['code' => 0, 'msg' => '成功', 'data' => $feedback, 'count' => $count]));
        }
        return View::fetch('feedback');
    }

    public function type(Request $request)
    {
        if ($request->isAjax()) {
            $get = $request->get();
            $page = $get['page'] - 1;
            $limit = $get['limit'];
            $feedback = Db::table('type')->order('id desc')->limit($page * $limit, $limit)->select();
            $count = Db::table('type')->count();
            exit(json_encode(['code' => 0, 'msg' => '成功', 'data' => $feedback, 'count' => $count]));
        }
        return View::fetch('type');
    }


    public function typeadd(Request $request)
    {
        if ($request->isAjax()) {
            $post = $request->post();
            $post['create_time'] = time();
            $type = Db::table('type')->insert($post);
            if ($type) {
                exit(json_encode(['code' => 1, 'msg' => '成功']));
            }
            exit(json_encode(['code' => 0, 'msg' => '添加失败']));
        }
    }

    public function typeedit(Request $request)
    {
        if ($request->isAjax()) {
            $post = $request->post();
            $id = $post['id'];
            $type = $post['type'];
            $state = Db::table('type')->where(['id' => $id])->update(['type' => $type]);
            if ($state) {
                exit(json_encode(['code' => 1, 'msg' => '成功']));
            } else {
                exit(json_encode(['code' => 0, 'msg' => '失败']));
            }
        }
    }
    public function typedel(Request $request)
    {
        if ($request->isAjax()) {
            $post = $request->post();
            $id = $post['id'];
            $state = Db::table('type')->where(['id' => $id])->delete();
            if ($state) {
                exit(json_encode(['code' => 1, 'msg' => '成功']));
            } else {
                exit(json_encode(['code' => 0, 'msg' => '失败']));
            }
        }
    }

    public function updatepassword(Request $request)
    {
        if ($request->isAjax()) {
            $post = $request->post();
            $user = $post['user'];
            $password = $post['password'];
            $state = Db::table('user')->where(['user' => $user])->update(['password' => md5($password)]);
            if ($state) {
                Cache::delete('username');
                exit(json_encode(['code' => 1, 'msg' => '成功', 'url' => '/admin']));
            } else {
                exit(json_encode(['code' => 0, 'msg' => '失败']));
            }
        }
        $user = Cache::get('username');
        View::assign('user', $user);
        return View::fetch('updatepassword');
    }

    public function userexit()
    {
        Cache::delete('username');
        exit("<script>location.reload()</script>");
    }
}
